package com.jinbo.footmessage.collection;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jinbo.footmessage.MyApplication;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.WorkerInformationBean;
import com.jinbo.footmessage.httpretrofit.HttpProtocolFactory;
import com.jinbo.footmessage.utils.LocalPreferencesHelper;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * Created by jinbo on 2015/8/19.
 */
public class CollectionListViewAdapter extends BaseAdapter {

    //TextView total;
    static class ViewHolder {
        TextView tv_name;
        CircleImageView head;
        TextView speciality;
        RatingBar star;
        TextView deleteTv;

    }
    
    private List<WorkerInformationBean> list = null;
    private Context context;
   // int sum;
  //  private View view;

    public CollectionListViewAdapter(Context context, List<WorkerInformationBean> list){//,View view) {//ArrayList<ProjectsBean> datalist) {
        this.context = context;
        this.list = list;
        //this.view=view;
      //  this.total=(TextView)view.findViewById(R.id.total_num);
    //    sum = 6;
    }
    public void updateListView(List<WorkerInformationBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

   /* void f()
    {
        sum=0;
       // total.setText(0+"");
    }*/
    @Override
    public int getCount() {
            // TODO Auto-generated method stub*//*
            if (list!= null) {
                return list.size();
            }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
            if (list!= null) {
                return list.get(position);
            }
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        ViewHolder holder = null;
        final WorkerInformationBean mContent = list.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_collection, null);
            holder = new ViewHolder();
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.head=(CircleImageView)convertView.findViewById(R.id.iv_head);
            holder.speciality=(TextView)convertView.findViewById(R.id.tv_speciality);
            holder.star=(RatingBar)convertView.findViewById(R.id.tv_star);
            holder.deleteTv = (TextView) convertView.findViewById(R.id.delete_btn);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.deleteTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String user_id=MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
                String s=MyApplication.local.getString(user_id+LocalPreferencesHelper.WORKER_IDS);
                MyApplication.local.saveOrUpdate(user_id + LocalPreferencesHelper.WORKER_IDS, removeWorkerId(s,mContent.id));
                list.remove(position);
                updateListView(list);
                //sum = sum - 1;
                //notifyDataSetChanged();
                //total.setText(""+(Integer.valueOf(total.getText().toString())-1));
            }
        });


        holder.tv_name.setText(mContent.name);

        Glide.with(context)
                .load(HttpProtocolFactory.HOST_URL_USER+mContent.getHead())
                        // .placeholder(R.mipmap.ic_launcher)
                        // .crossFade()
                .into(holder.head);
        holder.speciality.setText(mContent.getSpeciality());
        holder.star.setRating((float) mContent.getStar());
        return convertView;
    }

    String removeWorkerId(String ids,String  id)
    {
        String [] strs=ids.split("-");
        String s="";
        for(int i=0;i<strs.length;i++)
        {
            if(strs[i].equals(id))
                strs[i]="-1";
        }
        for(int i=0;i<strs.length;i++)
            if(!strs[i].equals("-1"))
                s=strs[i]+"-";
      /*  if(!s.equals("")) {
            int length = s.length();
            return s.substring(0, length - 1);
        }
        else*/
        return s;
    }
}
